import 'package:flutter/material.dart';
import 'Tab/Home.dart';
import 'Tab/Find.dart';
import 'Tab/Message.dart';
import 'Tab/My.dart';

class MainPage extends StatefulWidget {
  MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

const Map _bottomNames = {
  'home': '易货宝',
  'find': '发现',
  // 'redcamera': '',
  'mess': '消息',
  'my': '我的',
};

class _MainPageState extends State<MainPage> {
  int _currentIndex = 0;
  final List<BottomNavigationBarItem> _bottomNavBarList = [];
  List _pageList = [Home(), Find(), Message(), My()];

  @override
  void initState() {
    super.initState();
    _bottomNames.forEach((key, value) {
      _bottomNavBarList.add(_BottomNavBarItem(key, value));
    });
  }

  //底部导航栏切换
  void onTabClick(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: this._pageList[this._currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: _bottomNavBarList,
        currentIndex: _currentIndex,
        onTap: onTabClick,
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Colors.red,
      ),
    );
  }

  BottomNavigationBarItem _BottomNavBarItem(String key, String value) {
    return BottomNavigationBarItem(
      icon: Image.asset(
        'image/${key}M.png',
        width: 24.0,
        height: 24.0,
      ),
      activeIcon: Image.asset(
        'image/${key}A.png',
        width: 24.0,
        height: 24.0,
      ),
      label: value,
    );
  }
}
